package com.hfengxiang.example.canvasrotatedemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class FlowLayout extends ViewGroup {
    private List<List<View>> allViews;
    private List<Integer> lineHeights;
    private int mHorizontalSpacing = 20;
    private int mVerticalSpacing = 10;

    public FlowLayout(Context context) {
        super(context);
        initMeasureParams();
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initMeasureParams();
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initMeasureParams();
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initMeasureParams();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        clearMeasureParams();
        int childCount = getChildCount();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        // 根据父亲给的宽高规格拿到自身的宽高
        int selfWidth = MeasureSpec.getSize(widthMeasureSpec)-paddingLeft-paddingRight;
        int selfHeight = MeasureSpec.getSize(heightMeasureSpec)-paddingTop-paddingBottom;

        int lineWidthUsed = 0;
        int lineHeight = 0;
        List<View> lineViews = new ArrayList<>();


        int parentNeedWidth = 0;
        int parentNeedHeight = 0;
        // 度量，先自己后孩子（ViewPager），先孩子后自己皆可
        // 先度量孩子
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            LayoutParams layoutParams = childView.getLayoutParams();
            int childWidthSpec = getChildMeasureSpec(widthMeasureSpec,
                    paddingLeft + paddingRight, layoutParams.width);
            int childHeightSpec = getChildMeasureSpec(heightMeasureSpec,
                    paddingTop + paddingBottom, layoutParams.height);
            childView.measure(childWidthSpec, childHeightSpec);
            // 获取子View的测量宽高
            int measuredWidth = childView.getMeasuredWidth();
            int measuredHeight = childView.getMeasuredHeight();

            // 当子View宽度加上横向间隔总和时需要换行，换行后新行的宽高数据重新累加计算
            if (measuredWidth + lineWidthUsed + mHorizontalSpacing > selfWidth) {
                lineHeights.add(lineHeight);
                allViews.add(lineViews);
                lineWidthUsed = 0;
                lineHeight = 0;
                lineViews = new ArrayList<>();
                parentNeedWidth = Math.max(lineWidthUsed + mHorizontalSpacing, parentNeedWidth);
                parentNeedHeight = parentNeedHeight + lineHeight + mVerticalSpacing;
            }

            // 每一行的宽度等于这一行上子View的宽度+横向间隔之和
            // 每一行的高度等于这一行所有View的高度中最高的那一个
            lineWidthUsed = lineWidthUsed + measuredWidth + mHorizontalSpacing;
            lineHeight = Math.max(lineHeight, measuredHeight);
            lineViews.add(childView);
            lineHeights.add(lineHeight);
            if (i == childCount - 1) {
                lineHeights.add(lineHeight);
                allViews.add(lineViews);
            }
        }

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        // 要设置具体的宽高
        int realWidth = widthMode == MeasureSpec.EXACTLY ? selfWidth : parentNeedWidth;
        int realHeight = heightMode == MeasureSpec.EXACTLY ? selfHeight : parentNeedHeight;
        setMeasuredDimension(realWidth, realHeight);
    }

    private void initMeasureParams() {
        allViews = new ArrayList<>();
        lineHeights = new ArrayList<>();
    }

    private void clearMeasureParams() {
        allViews.clear();
        lineHeights.clear();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int curLeft = getPaddingLeft();
        int curTop = getPaddingTop();
        for (int i = 0; i < allViews.size(); i++) {
            List<View> lineViews = allViews.get(i);
            int height = lineHeights.get(i);
            for (int j = 0; j < lineViews.size(); j++) {
                View view = lineViews.get(j);
                int right = curLeft + view.getMeasuredWidth();
                int bottom = curTop + height;
                view.layout(curLeft, curTop, right, bottom);
                // 新的View top不变，left累加
                curLeft = right + mHorizontalSpacing;
            }
            // 新的一行top累加，left复位
            curTop = curTop + height + mVerticalSpacing;
            curLeft = getPaddingLeft();
        }
    }
}
